<template>
  <view>
    <!-- 搜索 -->
    <search />
    <!-- 分类 -->
    <view class="category">
      <!-- 顶级分类 -->
      <view class="sup">
        <scroll-view scroll-y>
          <!-- <text class="active">大家电</text> -->
          <text
            v-for="(item, index) in categoryDataList"
            :key="item.goods_id"
            :class="{ active: index === isActive }"
            @click="handleActiveClass(index)"
          >
            {{ item.cat_name }}
          </text>
        </scroll-view>
      </view>
      <!-- 子级分类 -->
      <view class="sub">
        <scroll-view scroll-y>
          <!-- 封面图 -->
          <image
            src="http://static.botue.com/ugo/uploads/category.png"
            class="thumb"
          />
          <view class="children" v-for="childItem in childCategoryDataList" :key="childItem.goods_id">
            <view class="title">{{ childItem.cat_name }}</view>
            <!-- 品牌 -->
            <view class="brands">
              <navigator
                v-for="grandSonItem in childItem.children"
                :key="grandSonItem.goods_id"
                :url="`/packone/list/index?query=${grandSonItem.cat_name}`"
              >
                <image :src="grandSonItem.cat_icon" />
                <text>{{ grandSonItem.cat_name }}</text>
              </navigator>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>
  </view>
</template>

<script>
import { getCategoryDataRequest } from '@/api/category.js'

export default {
  data() {
    return {
      // 分类列表数据
      categoryDataList: [],
      // 是否选中顶级分类的一个索引，目的是显示高亮，默认第一个高亮（即下标是0）
      isActive: 0
    }
  },

  computed: {
    // 根据当前选中的父级分类索引，得到子级分类列表数据
    childCategoryDataList() {
      return this.categoryDataList[this.isActive]?.children
    }
  },

  onLoad() {
    this.getCategoryData()
  },

  methods: {
    async getCategoryData() {
      const { data } = await getCategoryDataRequest()
      this.categoryDataList = data
    },

    // 点击一级分类大标题后，高亮显示
    handleActiveClass(index) {
      // index 拿到点击的一级分类的大标题的数组下标
      this.isActive = index
    }
  }
};
</script>

<style scoped lang="scss">
scroll-view {
  height: 100%;
}

.category {
  display: flex;
  width: 100%;
  position: absolute;
  top: 100rpx;
  bottom: 0;

  .sup {
    width: 196rpx;
    background-color: #f4f4f4;

    text {
      display: block;
      height: 100rpx;
      text-align: center;
      line-height: 100rpx;
      font-size: 27rpx;
      color: #333;
      border-bottom: 1rpx solid #eee;

      &:last-child {
        border-bottom: none;
      }

      &.active {
        background-color: #fff;
        color: #ea4451;
        position: relative;

        &::before {
          content: "";
          display: block;
          width: 8rpx;
          height: 60rpx;
          transform: translateY(-50%);
          background-color: #ea4451;

          position: absolute;
          left: 0;
          top: 50%;
        }
      }
    }
  }

  .sub {
    flex: 1;
    padding: 20rpx 18rpx;

    .thumb {
      width: 100%;
      height: 180rpx;
    }

    .children {
      text-align: center;
      color: #333;

      .title {
        display: inline-block;
        margin: 40rpx 0 20rpx;
        font-size: 30rpx;

        &::before {
          content: "/";
          margin-right: 20rpx;
          color: #666;
        }

        &::after {
          content: "/";
          margin-left: 20rpx;
          color: #666;
        }
      }
    }

    .brands {
      display: flex;
      flex-wrap: wrap;

      navigator {
        width: 33%;
        margin-bottom: 20rpx;
      }

      image {
        width: 120rpx;
        height: 120rpx;
      }

      text {
        display: block;
        font-size: 24rpx;
      }
    }
  }
}
</style>
